package cn.iocoder.yudao.module.dine.config.service.core;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.MerchantExtensionService;
import cn.iocoder.yudao.framework.business.basic.service.impl.AbstractService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.dine.vo.DineConfigVO;
import cn.iocoder.yudao.module.api.product.spu.enums.ProductDeliveryScopeEnum;
import cn.iocoder.yudao.module.api.product.spu.enums.ProductPriceModeEnum;
import cn.iocoder.yudao.module.api.trade.delivery.enums.DeliveryTypeEnum;
import cn.iocoder.yudao.module.dine.config.dao.DineConfigMapper;
import cn.iocoder.yudao.module.dine.config.pojo.po.DineConfig;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/08/01
 */
@Service
public class CoreDineConfigService extends AbstractService<DineConfig, DineConfigVO> implements MerchantExtensionService<DineConfig> {
    @Resource
    private DineConfigMapper dineConfigMapper;

    @Override
    public DineConfigMapper getInjectedMapper() {
        return dineConfigMapper;
    }

    @DataPermission(enable = false)
    @Override
    public DineConfig getOrCreate(Long shopId) {
        CoreLambdaQueryWrapper<DineConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(DineConfig::getShopId, shopId);
        wrapper.orderByDesc(DineConfig::getId);
        wrapper.limitN(1);
        DineConfig entity = dineConfigMapper.selectOne(wrapper);
        if (entity == null) {
            entity = new DineConfig();
            entity.setShopId(shopId);
            entity.setPriceMode(ProductPriceModeEnum.SINGLE_PRODUCT.getValue());
            entity.setWeightUnitName("");
            entity.setMeatUnitPrice(0);
            entity.setVegetableUnitPrice(0);
            entity.setConsumeThresholdPrice(0);
            entity.setDeliveryTypes(List.of(DeliveryTypeEnum.NONE.getType()));
            entity.setDeliveryScope(ProductDeliveryScopeEnum.ADDRESS.getValue());
            entity.setDeliveryRadius(0);
            entity.setDeliveryAddresses(List.of());
            entity.setDeliveryPrice(0);
            entity.setDeliveryFreeThresholdPrice(0);
            entity.setRequireStapleFood(false);
            entity.setMaxDailyOrders(-1);
            dineConfigMapper.insert(entity);
        }
        return entity;
    }

    public List<DineConfig> getList(Set<Long> shopIds) {
        if (CollUtil.isEmpty(shopIds)) {
            return List.of();
        }
        CoreLambdaQueryWrapper<DineConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.in(DineConfig::getShopId, shopIds);
        return dineConfigMapper.selectList(wrapper);
    }

}
